package cn.edu.scau.cmi.ema.util;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

public class CmiJPAUtil {
	public static void main(String[] args) throws Throwable {
		String unitName = args[0];
		EntityManagerFactory emf = Persistence.createEntityManagerFactory(unitName);
		EntityManager em = emf.createEntityManager();

		BufferedReader reader =  new BufferedReader(new InputStreamReader(System.in));
		for(;;) {
			System.out.println("JPQL> ");
			String query = reader.readLine();
			if(query.equals("quit")) {
				break;
			}
			if(query.length() == 0) {
				continue;
			}
			try {
				List result = em.createQuery(query).getResultList();
			}catch(Exception e) {
				e.printStackTrace();
			}
		}

	}

}
